package light.head.dao.site;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.News;
import light.head.model.NewsItem;

import org.springframework.stereotype.Service;

@Service
public class NewsDAO extends BaseDAO<News, Integer>{
	private static final String sql_queryBidList = "FROM NewsItem ni WHERE ni.uc = ? AND ni.deleted = 0";
	private static final String sql_List = "FROM NewsItem WHERE url = ?";
	private static final String sql_list = "select *,COUNT(distinct s.starturl) from News s group by s.starturl";
	public RESULT findByMap(Map<String, String> map, PageBean pageBean){
		StringBuffer hql = new StringBuffer("FROM News n WHERE 1 = 1");
		List<Object> list = new ArrayList<Object>();
		if (map != null) {
			String starturl = map.get(Parm.STARTURL);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String remark = map.get(Parm.REMARK);
			String deleted = map.get(Parm.DELETED);
			
			if (starturl != "" && isNotNull(starturl)) {
				hql.append(" AND n.starturl LIKE ? ");
				list.add("%" + starturl + "%");
			}
			if (remark != "" && isNotNull(remark)) {
				hql.append(" AND n.remark LIKE ? ");
				list.add("%" + remark + "%");
			}
			if (deleted != "" && isNotNull(deleted)) {
				hql.append(" AND n.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
			if (time1 != "" && isNotNull(time1)) {
				hql.append(" AND n.ct >= ? ");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}
			if (time2 != "" && isNotNull(time2)) {
				hql.append(" AND n.ct <= ?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}
		}
		hql.append(" ORDER BY n.code DESC");

		return findToPageBean(hql.toString(), pageBean, list);
	} 
	public List<NewsItem> findUc(Integer uc){
		if (uc == null || uc == 0) return null;
		List<NewsItem> list = find(sql_queryBidList, uc);
		return list;
	}
	public List<NewsItem> findAll(String link){
		List<NewsItem> list = find(sql_List,link);
		return list;
	}
	public List<News> find(){
		List<News> list = find(sql_list);
		return list;
	}
}
